//
//  ViewController.m
//  HomeWork-23（加密解密）
//
//  Created by 鹏飞罗 on 16/8/16.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "NSString+LSTMD5.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *enText;
@property (weak, nonatomic) IBOutlet UITextField *deText;
@property (nonatomic,strong) AFHTTPSessionManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.manager = [AFHTTPSessionManager manager];
    
    
}
- (IBAction)enButton:(id)sender {
    self.manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    NSString *enString = _enText.text;
    NSString *postString = @"http://tool.zzblo.com/api/md5/encrypt";
    NSString *paraments = [NSString stringWithFormat:@"text=%@",enString];
    self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [self.manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return paraments;
    }];
    [self.manager POST:postString parameters:paraments progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"加密成功%@", responseObject);
        _deText.text = [NSString stringWithFormat:@"%@", responseObject[@"secret16"]];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

- (IBAction)deButton:(id)sender {
    self.manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [self.manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    NSString *postString = @"http://tool.zzblo.com/api/md5/decrypt";
    NSString *paraments = [NSString stringWithFormat:@"secret=%@",_deText.text];
    [self.manager POST:postString parameters:paraments progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"解密成功%@", responseObject);
        _enText.text = responseObject[@"text"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
}

- (IBAction)changeMD5Button:(id)sender {
    NSString *string = _deText.text;
   _deText.text = [string stringWith16BitMD5Lower];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
